package com.mgxy.GlodMiner;

import java.awt.*;

public class Line {

    //起点坐标
    int startx=380;
    int starty=180;
    //终点坐标
    int endx=500;
    int endy=500;
    //线长
    double length = 100;
    double MIN_length = 100;
    double MAX_length = 750;
    double degree = 0;
    //方向
    int dir = 1;
    //状态 0：摇摆   1：抓取    2：收回    3:抓取返回
    int state;
    //钩爪图片
    Image hook = Toolkit.getDefaultToolkit().getImage(Line.class.getClassLoader().getResource("resources/hook.png"));

    GameWin frame;

    Line(GameWin frame){
        this.frame = frame;
    }


    //触碰判断，检测是否抓取到物品
    void logic(){
        for (Object obj : this.frame.objectList ){
            if(endx>obj.x && endx<obj.x+obj.width
                    && endy>obj.y && endy<obj.y+obj.height){
                state=3;
                obj.flag = true;
            }
        }
    }

//绘制
    void lines(Graphics g){
        endx = (int) (startx + length*Math.cos(degree * Math.PI));
        endy = (int) (starty + length*Math.sin(degree * Math.PI));
        g.setColor(Color.red);
        g.drawLine(startx-1,starty-1,endx,endy);
        g.drawLine(startx,starty,endx,endy);
        g.drawLine(startx+1,starty+1,endx,endy);
        g.drawImage(hook,endx-36,endy-2,null);
    }


    void paintSelf(Graphics g){
        logic();
        switch (state){
            case 0:
                if(degree<0.1){
                    dir = 1;
                }else if(degree>0.9){
                    dir = -1;
                }
                degree = degree + 0.005 * dir;
                lines(g);
                break;
            case 1:
                if(length<MAX_length){
                    length+=6;
                    lines(g);
                }else {
                    state = 2;
                }
                break;
            case 2:
                if(length>MIN_length){
                    length-=5;
                    lines(g);
                }else {
                    state=0;
                }
                break;
            case 3:
                int m=1;
                if(length>MIN_length){
                    length-=5;
                    lines(g);
                    for (Object obj : this.frame.objectList ){
                        if (obj.flag){
                            m=obj.m;
                            obj.x=endx- obj.getWidth()/2;
                            obj.y=endy;
                            if(length<=MIN_length){
                                obj.x=-150;
                                obj.y=-150;
                                obj.flag=false;
                                //加分
                                Bg.count+=obj.count;
                                Bg.waterFlag=false;
                                state=0;
                            }
                            if(Bg.waterFlag){
                                if (obj.objtype==1){
                                m=1;
                                }
                                if (obj.objtype==2){
                                    obj.x=-150;
                                    obj.y=-150;
                                    obj.flag=false;
                                    Bg.waterFlag=false;
                                    state=2;
                                }
                            }
                        }
                    }
                }
                try {
                    Thread.sleep(m);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }
    //重置线
    void reGameLine(){
        degree=0;
        length=100;
    }
}
